//
//  UIViewController+BackButtonItem.m
//  YBInsurance
//
//  Created by S2 on 7/18/16.
//  Copyright © 2016 YB. All rights reserved.
//

#import "UIViewController+BackButtonItem.h"

@implementation UIViewController (BackButtonItem)



- (void)setupDifferentUserNotification {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popToRootVC) name:@"YBDifferentUserNotification" object:nil];
}

- (void)popToRootVC {
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self.navigationController popToRootViewControllerAnimated:YES];
    });
}

- (void)setupLeftBarItemWithImageName:(NSString *)imageName {
    
    NSString *imgName = imageName == nil ? @"shangpinglibiao-tb-fanhuianniu" : imageName;
    
    UIButton *leftButton =[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 36)];
    [leftButton addTarget:self action:@selector(popVC) forControlEvents:UIControlEventTouchUpInside];

    [leftButton setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    
    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftButtonItem;
}

- (void)popVC {
    
    if ([self.navigationController.viewControllers.firstObject isEqual:self] && self.presentingViewController != nil) {//当前页面尚未被Push过
    
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self dismissViewControllerAnimated:YES completion:nil];
        });
    } else {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.navigationController popViewControllerAnimated:YES];
        });
    }
}

- (void)setupLeftBarItemWithTitle:(NSString *)title action:(SEL)action {
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:action];
    rightButtonItem.tintColor = YBMidTitleColor;
    self.navigationItem.leftBarButtonItem = rightButtonItem;
}

- (void)setupRightBarItemWithTitle:(NSString *)title action:(SEL)action {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, 36, 44);
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:ISOrangeColor forState:UIControlStateNormal];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = leftButtonItem;
    
//    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:action];
//    rightButtonItem.tintColor = ISOrangeColor;
//    self.navigationItem.rightBarButtonItem = rightButtonItem;
}

- (void)setupRightBarItemWithImageName:(NSString *)imageName action:(SEL)action {
    
    UIImage *image = [UIImage imageNamed:imageName];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:action];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
}

- (UIButton *)setupLeftBarItemWithTitle1:(NSString *)title1 title2:(NSString *)title2 action:(SEL)action {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, 44, 44);
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button setTitle:title1 forState:UIControlStateNormal];
    [button setTitle:title2 forState:UIControlStateSelected];
    [button setTitleColor:YBMidTitleColor forState:UIControlStateNormal];
    [button setTitleColor:YBCyanColor forState:UIControlStateSelected];
    [button setTintColor:[UIColor clearColor]];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = leftButtonItem;
    return button;
}

- (UIButton *)setupRightBarItemWithTitle1:(NSString *)title1 title2:(NSString *)title2 action:(SEL)action {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, 44, 44);
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button setTitle:title1 forState:UIControlStateNormal];
    [button setTitle:title2 forState:UIControlStateSelected];
    [button setTitleColor:YBMidTitleColor forState:UIControlStateNormal];
    [button setTitleColor:YBCyanColor forState:UIControlStateSelected];
    [button setTintColor:[UIColor clearColor]];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    return button;
}





@end
